package com.example.flight.service;

import com.alibaba.fastjson.JSONObject;
import com.example.flight.generate.CityBean;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: leo
 * @date: 2021/7/2
 */
public class CityService {

    public static Map<String,String> getCityCode(){
        String[] city = new String[]{"鞍山","北京","成都","贵阳","哈尔滨","杭州","合肥","济南","昆明","南京","南宁","上海","深圳","沈阳","石家庄","太原","天津","温州","武汉","西安","银川","长春","郑州","重庆","青岛","赣州","临沂","南通","宁波"};
        Map<String,String> map = new HashMap<>();
        for (int i = 0; i < city.length; i++) {
            String c = city[i];
            CityBean cityBean = null;
            try {
                cityBean = httpPost(c).toJavaObject(CityBean.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String code = cityBean.getResponse().getData().get(0).getCode();
            map.put(c,code);
        }
        return map;
    }


    public static void main(String[] args) throws IOException {
        Map<String, String> cityCode = getCityCode();
        System.out.println(cityCode);
    }

    public static JSONObject httpPost(String cityName) throws IOException {
        String url = "https://ct.ctrip.com/frontendPub/poiSearch/flight";
        // 获取请求连接
        Connection con = Jsoup.connect(url).ignoreContentType(true).ignoreHttpErrors(true);
        String jsonContent = "{" +
                "\"KeyWord\": "+"\""+cityName+"\""+",\n" +
                "\"Mode\": 1,\n" +
                "\"Language\": \"zh-cn\",\n" +
                "\"Stoken\": \"aaab3be1-9a10-421f-821b-8831c6755189\"\n" +
                "}";

        con.requestBody(jsonContent);
        // 插入cookie（头文件形式）
//        String cookie = "JSESSIONID=7A9F2D1BBC22812A7A8332801179A498; Hm_lvt_978e41b5a436a203cd8c50b1972f5e13=1625206593; Hm_lpvt_978e41b5a436a203cd8c50b1972f5e13=1625206593; _ga=GA1.2.1865223152.1625206593; _gid=GA1.2.946453231.1625206593; Corp_ResLang=zh-cn; cticket=B5B63D43309E23DD7365076DF9383566FD892AD9510622F72E3C62CCFA733533; ticket_ctrip=bJ9RlCHVwlu1ZjyusRi+ypZ7X2r4+yojyzYEMjGgdJUY+8MkQJWJwZJ1AGK2Pjd4zAAQPp94LF4w/WqKJcY6KhLA7ZzlVmewX6OS1RQYrFXoQdK/FX7NnEIplnGphCiDWSsBM32tWQ4NlgkBJeuEBkE5/m3egAtxn9c5l1eeXvXxv7/yyswEuQHX59ga6XcuILLXPrrZUA+mjfBjrmfzaT5KiwcwfQ4YBR71Sb691WuCU02eRGslJMnZmc2cDfwctBdHNfCRsnqPbWRg2gfMgJdj6uM7qoeJ2gPJj8Nxdk4=; ASP.NET_SessionSvc=MTAuNjAuNDkuNzh8OTA5MHxqaW5xaWFvfGRlZmF1bHR8MTYyMzE0MzgyNzY5MQ; SSOkey=09a7fa4e-74bd-4352-ad8e-a104d4999e7d; _IsShowInfo=true; DUID=u=DC1A448F0309B3B22AE165F1E7445D13&v=0; sToken=a3cc058a29784e29b650df95ae5e884a; IsFristShowInfo=2130126491; isShowCorpTip=yes; GUID=09031049415197996288; _bfa=1.1625206593140.q9quf.1.1625206593140.1625206593140.1.6; _bfs=1.6; _bfi=p1%3D10650045156%26p2%3D10650045156%26v1%3D6%26v2%3D5";
//        con.header("cookie", cookie);
//        con.header(
//                "user-agent",
//                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36");
        Document doc = con.header("Content-Type", "application/json").post();
        Element body = doc.body();
        JSONObject json = JSONObject.parseObject(body.text());
        return json;
    }

}
